//
//  YHAlertView.m
//  YiBaoStore
//
//  Created by 毛云河 on 2018/12/4.
//  Copyright © 2018 小河. All rights reserved.
//

#import "YHAlertView.h"

@interface YHAlertView ()

@property (nonatomic, strong) UIButton *mainBtnIcon;
@property (nonatomic, strong) UITextField *costTF;//成本价
@property (nonatomic, strong) UITextField *saleTF;//销售价
@property (nonatomic, strong) UITextField *storesTF;//库存

@end

@implementation YHAlertView

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bgNavView.hidden = YES;
}

#pragma mark - 展示弹出框
- (void)showAlertView
{
    UIWindow * window = [[[UIApplication sharedApplication]delegate]window];
    [window addSubview:self.view];
}

#pragma mark - 关闭弹窗
- (void)closeBtnClick
{
    [self removeAlerrView];
}

#pragma mark - 移除弹框
- (void)removeAlerrView
{
  [self.view removeFromSuperview];
}


- (instancetype)initPutawayAlertViewWithModel:(MainModel *)model
{
    self = [super init];
    if (self) {
        [self createPutawayAlertViewWithModel:model];
    }
    return self;
}

- (void)createPutawayAlertViewWithModel:(MainModel *)model
{
    self.view.backgroundColor = RGBACOLOR(0, 0, 0, 0.3);
    
    UIView *bgView = [[UIView alloc] init];
    [self.view addSubview:bgView];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.center = self.view.center;
    bgView.bounds = CGRectMake(0, 0, 275, 470);
    bgView.layer.cornerRadius = 5;
    bgView.layer.masksToBounds = YES;
    
    UIImageView *closeIcon = [[UIImageView alloc] init];
    [bgView addSubview:closeIcon];
    closeIcon.frame = CGRectMake(bgView.width-10-16, 10, 16, 16);
    closeIcon.image = [UIImage imageNamed:@"closeIcon"];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [bgView addSubview:closeBtn];
    closeBtn.frame = CGRectMake(bgView.width-40, 0, 40, 40);
    [closeBtn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    [bgView addSubview:titleLabel];
    titleLabel.frame = CGRectMake((bgView.width-90)/2, 20, 90, 14);
    titleLabel.font = FontWithSize(14);
    titleLabel.textColor = BlackTextColor;
    titleLabel.textAlignment = 1;
    titleLabel.text = @"确认上架信息";
    
    UIImageView *mainIcon = [[UIImageView alloc] init];
    [bgView addSubview:mainIcon];
    mainIcon.frame = CGRectMake((bgView.width-150)/2, titleLabel.bottom+20, 150, 150);
    mainIcon.backgroundColor = RGBACOLOR(248, 248, 248, 1);
    [mainIcon sd_setImageWithURL:[NSURL URLWithString:model.showImage]];
    mainIcon.clipsToBounds = YES;
    mainIcon.contentMode = UIViewContentModeScaleAspectFill;
    
//    self.mainBtnIcon = [UIButton buttonWithType:UIButtonTypeCustom];
//    [bgView addSubview:self.mainBtnIcon];
//    self.mainBtnIcon.userInteractionEnabled = NO;
//    self.mainBtnIcon.frame = CGRectMake((bgView.width-150)/2, titleLabel.bottom+20, 150, 150);
//    self.mainBtnIcon.backgroundColor = RGBACOLOR(248, 248, 248, 1);
//    [self.mainBtnIcon sd_setImageWithURL:[NSURL URLWithString:model.showImage] forState:UIControlStateNormal];
    
    UIView *textBgView = [[UIView alloc] init];
    [bgView addSubview:textBgView];
    textBgView.frame = CGRectMake(12, mainIcon.bottom+15, bgView.width-24, 60);
//    textBgView.backgroundColor = RGBACOLOR(252, 252, 252, 1);
//    textBgView.layer.borderWidth = 1;
//    textBgView.layer.borderColor = RGBACOLOR(239, 239, 239, 1).CGColor;
    
    
    UITextView *titleTF = [[UITextView alloc] init];
    [textBgView addSubview:titleTF];
    titleTF.frame = CGRectMake(8, 5, textBgView.width-16, textBgView.height-10);
    titleTF.font = FontWithSize(14);
    titleTF.textColor = BlackTextColor;
    titleTF.textAlignment = 0;
    titleTF.text = model.title;
    titleTF.userInteractionEnabled = NO;
    
    for (int i = 0; i < 3; i++)
    {
        UILabel *titleLabel = [[UILabel alloc] init];
        [bgView addSubview:titleLabel];
        titleLabel.frame = CGRectMake(17+(55)*(i%1), (textBgView.bottom+22)+(17+18)*(i/1), 55, 17);
        titleLabel.textAlignment = 0;
        titleLabel.textColor = BlackTextColor;
        titleLabel.font = FontWithSize(12);
        
        UIView *tfBgView = [[UIView alloc] init];
        [bgView addSubview:tfBgView];
        tfBgView.frame = CGRectMake(82+(180)*(i%1), (textBgView.bottom+15)+(30+5)*(i/1), 180, 30);
//        tfBgView.backgroundColor = RGBACOLOR(252, 252, 252, 1);
//        tfBgView.layer.borderWidth = 1;
//        tfBgView.layer.borderColor = RGBACOLOR(239, 239, 239, 1).CGColor;
        
        UITextField *TF = [[UITextField alloc] init];
        [tfBgView addSubview:TF];
        TF.frame = CGRectMake(12, 0, tfBgView.width-24, 30);
        TF.textAlignment = 0;
        TF.textColor = BlackTextColor;
        TF.font = FontWithSize(12);
        TF.userInteractionEnabled = NO;
        
        switch (i) {
            case 0:
            {
                titleLabel.text = @"  成 本 价";
                CGFloat shopCost = [model.shopCostPrice floatValue];
                TF.text = [NSString stringWithFormat:@"%0.2f",shopCost];
                self.costTF = TF;
            }
                
                break;
                
            case 1:
            {
                titleLabel.text = @"  销 售 价";
                CGFloat shopPrice = [model.price floatValue];
                TF.text = [NSString stringWithFormat:@"%0.2f",shopPrice];
                self.saleTF = TF;
            }
                break;
                
            case 2:
            {
                titleLabel.text = @"  库    存";
                NSInteger stockAmount_number = [model.stockAmount integerValue];
                TF.text = [NSString stringWithFormat:@"%ld",stockAmount_number];
                self.storesTF = TF;
            }
                break;
                
            default:
                break;
        }
    }
    
    UIView *lineTop = [[UIView alloc] init];
    [bgView addSubview:lineTop];
    lineTop.backgroundColor = RGBACOLOR(204, 204, 204, 1);
    lineTop.frame = CGRectMake(0, bgView.height-45, bgView.height, 1);
    
    UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [bgView addSubview:editBtn];
    editBtn.frame = CGRectMake(0, bgView.height-44, bgView.width/2-1, 44);
    editBtn.backgroundColor = [UIColor whiteColor];
    editBtn.titleLabel.font = FontWithSize(12);
    [editBtn setTitle:@"去编辑" forState:UIControlStateNormal];
    [editBtn setTitleColor:BlackTextColor forState:UIControlStateNormal];
    [editBtn addTarget:self action:@selector(gotoEditClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *lineView = [[UIView alloc] init];
    [bgView addSubview:lineView];
    lineView.backgroundColor = RGBACOLOR(204, 204, 204, 1);
    lineView.frame = CGRectMake(editBtn.right, bgView.height-44, 1, 44);
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [bgView addSubview:sureBtn];
    sureBtn.frame = CGRectMake(bgView.width/2, bgView.height-44, bgView.width/2, 44);
    sureBtn.titleLabel.font = FontWithSize(12);
    [sureBtn setTitle:@"确认上架" forState:UIControlStateNormal];
    [sureBtn setTitleColor:BlackTextColor forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    if (NOEmptyStr(model.showImage)&&
        NOEmptyStr(model.title)&&
        NOEmptyStr(model.shopCostPrice)&&
        NOEmptyStr(model.price)&&
        NOEmptyStr(model.stockAmount))
    {
        sureBtn.backgroundColor = BtnBgColor;
        sureBtn.userInteractionEnabled = YES;
    }else {
        sureBtn.backgroundColor = [UIColor lightGrayColor];
        sureBtn.userInteractionEnabled = NO;
    }
    
}

//- (void)selectPhotoBtnClick:(UIButton *)sender
//{
//    [self takePhoto];
//    __weak YHAlertView *myself = self;
//    self.photoBlock = ^(UIImage * _Nonnull image)
//    {
//        [myself.mainBtnIcon setBackgroundImage:image forState:UIControlStateNormal];
//    };
//}

- (void)sureBtnClick
{
    if (self.BtnBlock)
    {
        self.BtnBlock(2);
    }
}

- (void)gotoEditClick
{
    if (self.BtnBlock)
    {
        [self.view removeFromSuperview];
        self.BtnBlock(1);
    }
}



@end
